from django.db import models
from django.utils import timezone

FACTORY_CHOICE = [
    ('F1', 'F1'),
    ('F2', 'F2'),
]

PROCEDURE_CHOICE = [
    ('加工', '加工'),
    ('涂装', '涂装')
]


# 报告文件 common
class FileRpt(models.Model):
    title = models.CharField(max_length=100)  # 原始文件名
    filename_saved = models.CharField(max_length=200)  # 保存的文件名： title+uuid
    note = models.TextField(max_length=500, blank=True)  # 备注
    version = models.PositiveIntegerField()  # 版本
    md5 = models.CharField(max_length=100, blank=True)
    created = models.DateTimeField(default=timezone.now)

    class Meta:
        abstract = True


# 加工/涂装
class RptJgTz(models.Model):
    factory = models.CharField(max_length=20, choices=FACTORY_CHOICE)
    product = models.CharField(max_length=100)
    procedure = models.CharField(choices=PROCEDURE_CHOICE, max_length=10)
    mold_no = models.CharField(max_length=50, blank=True)  # 模具号
    time_of_incident = models.DateField(blank=True)  # 发生时间
    problem = models.CharField(max_length=500)
    improvement = models.CharField(max_length=500)  # 改进
    stage = models.DateField(max_length=100)  # 改善时间段
    before = models.CharField(max_length=100)  # 改善前
    after = models.CharField(max_length=100)  # 改善后
    is_ok = models.BooleanField()  # 改善结果判定
    note = models.CharField(max_length=200, blank=True)  # 备注


class FileRptJgTz(FileRpt):
    jgtz_log = models.ForeignKey(RptJgTz, on_delete=models.CASCADE)


# 社外改良
class RptImprove(models.Model):
    customer_agency = models.CharField(max_length=50)  # 顾客社
    contact = models.CharField(max_length=20)  # 联系人
    time_of_incident = models.DateField(blank=True)  # 发生时间
    time_of_receive = models.DateField(blank=True)  # 接收时间

    product = models.CharField(max_length=100)  # 品名
    num = models.PositiveIntegerField()  # 发生数量
    problem = models.CharField(max_length=500)
    info_lot = models.CharField(max_length=100)  # LOT信息
    loc_of_incident = models.CharField(max_length=50)  # 发生问题共序

    improvement_temporary = models.CharField(max_length=500)  # 改进 临时
    improvement_essential = models.CharField(max_length=500)  # 改进 根本
    is_ok = models.BooleanField()  # 改善结果判定
    note = models.CharField(max_length=200, blank=True)  # 备注


class FileRptImprove(FileRpt):
    improve_log = models.ForeignKey(RptImprove, on_delete=models.CASCADE)


# 社外工程检查
class RptInspection(models.Model):
    customer_agency = models.CharField(max_length=50)  # 顾客社
    inspector = models.CharField(max_length=20)  # 检查员
    time_of_inspection = models.CharField(max_length=50)  # 检查时间
    item = models.CharField(max_length=100)  # 检查项目

    is_feedback = models.BooleanField()  # 改善对策是否反馈完成
    is_ok = models.BooleanField()  # 改善结果判定
    note = models.CharField(max_length=200, blank=True)  # 备注


# 社外工程检查报告文件
class FileRptInspection(FileRpt):
    inspection_log = models.ForeignKey(RptInspection, on_delete=models.CASCADE)
